package com.yunpeng.aidevice.common.controllers;

import com.alibaba.fastjson.JSONObject;
import com.yunpeng.aidevice.biz.service.IDictionariesBizService;
import com.yunpeng.common.dto.Result;
import com.yunpeng.common.paging.PagingQueryDTO;
import com.yunpeng.common.util.ParametersCheckUtil;
import com.yunpeng.aidevice.common.vo.DictionariesVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * DictionariesController 字典
 *
 * @author : Moore(hemw)
 * @date : 2020-04-13
 */
@RestController
@RequestMapping("/api/dictionaries")
public class DictionariesController {

    @Resource
    private IDictionariesBizService dictionariesBizService;

    /**
     * 字典数据查询
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "queryList", method = RequestMethod.POST)
    public Result<List<DictionariesVO>> queryList(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String domain = requestParam.getString("domain");
        String group = requestParam.getString("group");
        ParametersCheckUtil.isNotBlank(domain, "字典业务域为空");
        ParametersCheckUtil.isNotBlank(group, "字典业务组为空");
        return dictionariesBizService.queryDictionaries(domain, group);
    }

    /**
     * 更新字典键
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateKey", method = RequestMethod.POST)
    public Result<Void> updateKey(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String domain = requestParam.getString("domain");
        String group = requestParam.getString("group");
        String key = requestParam.getString("key");
        String value = requestParam.getString("value");
        ParametersCheckUtil.isNotBlank(domain, "字典业务域为空");
        ParametersCheckUtil.isNotBlank(group, "字典业务组为空");
        ParametersCheckUtil.isNotBlank(key, "字典键为空");
        ParametersCheckUtil.isNotBlank(value, "字典值为空");
        return dictionariesBizService.updateDictionaries(domain, group, key, value);
    }


    /**
     * 字典分页列表
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public Result<PagingQueryDTO> list(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String domain = requestParam.getString("domain");
        String group = requestParam.getString("group");
        String key = requestParam.getString("key");
        Integer pageSize = requestParam.getInteger("pageSize");
        Integer currentPage = requestParam.getInteger("currentPage");
        return dictionariesBizService.queryDictionariesList(domain, group, key, pageSize, currentPage);
    }

}
